package com.zhiyou100.controller;

import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhiyou100.model.User;
import com.zhiyou100.model.result;
import com.zhiyou100.service.UserService;

@Controller
@RequestMapping("/front/user")
public class UserController {

	@Autowired
	private UserService us;
	
	
	@RequestMapping("/logout.do")
	public String logout(HttpSession session,HttpServletResponse response) {
		session.setAttribute("_front_user", null);
		return "redirect:/";
	}
	
	@RequestMapping("/index.do")
	public String index(HttpSession session,HttpServletResponse response) {
		session.setAttribute("user", session.getAttribute("_front_user"));
		return "redirect:../user/index.jsp";
	}
	
	
	
	@RequestMapping("/login.do")
	@ResponseBody
	public result login(User user,HttpSession session,HttpServletResponse response){
		result rs = new result();
		if(us.selectUserByEmail(user.getEmail())==null) {
			rs.message="用户名错误，请重新输入";
		}else {
			user.setPassword(DigestUtils.md5Hex(user.getPassword()));
			User u=us.userLogin(user);
			if(u!=null) {
			session.setAttribute("_front_user", u);
			rs.success="true";
			}else{
				rs.message="密码错误，请重新输入";
			}
		}
		return rs;
	}
	

	
	@RequestMapping("/regist.do")
	@ResponseBody
	public result register(User user,HttpSession session,HttpServletResponse response){
		result rs = new result();
		User u = us.selectUserByEmail(user.getEmail());
		if(u!=null) {
		System.out.println(u.getNick_name());
		rs.message="邮箱已被注册，请直接登录或者更换邮箱注册";
		}else {
			user.setPassword(DigestUtils.md5Hex(user.getPassword()));
			user.setInsert_time(new Date());
			int b = us.addUser(user);	
			if(b>0) {
				session.setAttribute("_front_user", user);
				rs.success="true";
			}else {
				rs.message="注册发生未知错误，请尝试重新注册";
			}
		}
		return rs;
	}
	
	
	@RequestMapping("/profile.do")
	public String profile(HttpSession session,HttpServletResponse response) {
		return "/front/user/profile";
	}
	
	@RequestMapping("/avatar.do")
	public String avatar(HttpSession session,HttpServletResponse response) {
		return "/front/user/avatar";
	}
	
	@RequestMapping("/password.do")
	public String password(HttpSession session,HttpServletResponse response) {
		return "/front/user/password";
	}
	
	@RequestMapping("/userUpdate.do")
	@ResponseBody
	public result userUpdate(User user,HttpSession session) {
		result rs = new result();
		user.setUpdate_time(new Date());
		System.out.println("用户"+user.getBirthday());
		int a = us.userUpdate(user);
		if(a>0) {
			session.setAttribute("user", user);
			session.setAttribute("_front_user", user);
		    rs.success="true";
		}else {
			rs.message="修改失败，请重试";
		}
		return rs;
	}
}
